#include "my_string.h"

void
my_qsort(void *base, size_t n, size_t size,
    int (*cmp)(const void *, const void *))
{
    u_char  *p1, *p2, *p;

    p = malloc(size);
    assert(p != NULL);

    for (p1 = (u_char *) base + size;
         p1 < (u_char *) base + n * size;
         p1 += size)
    {
        MY_MEMCPY(p, p1, size);

        for (p2 = p1;
             p2 > (u_char *) base && cmp(p2 - size, p) > 0;
             p2 -= size)
        {
            MY_MEMCPY(p2, p2 - size, size);
        }

        MY_MEMCPY(p2, p, size);
    }

    free(p);
}